package branchandbound;

import java.util.*;

public class ShipPro {
    //总重量
    static int Weight;
    //物品个数
    static int count;
    // 每个数量
    static int[] perWeight;
    //物品民称
    static String[] name;
    //封装成List<Object>对象
    static List<Baobei> list;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Weight = input.nextInt();
        count = input.nextInt();
        input.nextLine();
        String[] erveryWeight = input.nextLine().split(" ");
        perWeight = new int[count];
        name = new String[count];
        for (int i = 0; i < erveryWeight.length; i++) {
            perWeight[i] = Integer.parseInt(erveryWeight[i]);
        }
        name = input.nextLine().split(" ");
        list = new ArrayList<Baobei>();

        for (int i = 0; i < count; i++) {
            list.add(new Baobei(perWeight[i],name[i]));
        }

        sort();
    }

    /**
     * 排序求和
     */
    private static void sort() {
        //当前总和重量
        int sum=0;
        //个数
        int currentCount=0;
        //...算了.....
        /*list.stream().sorted().map().forEach(System.out::println);*/
        //排序重写baobei Comparable代码
        Collections.sort(list);
        for (int i = 0; i < list.size(); i++) {
            if(sum+list.get(i).getCurrentWeight()<Weight){
                sum+=list.get(i).getCurrentWeight();
                System.out.print(list.get(i).getCurrentName()+" ");
                currentCount++;
            }
        }
        System.out.println("\n"+currentCount);
    }


}

/**
 * 自定义排序compareTo 方便Collections.sort;
 */
class Baobei implements Comparable<Baobei>{
    private int currentWeight;
    private String currentName;

    public Baobei(int currentWeight, String currentName) {
        this.currentWeight = currentWeight;
        this.currentName = currentName;
    }

    public int getCurrentWeight() {
        return currentWeight;
    }


    public String getCurrentName() {
        return currentName;
    }


    @Override
    public int compareTo(Baobei o) {
        return this.currentWeight-o.getCurrentWeight();
    }
}
